import http.client
import re
import os
import datetime
import sys
import utils

p_html_anime = re.compile(r'(fiche-anime-.*?\.html).*?img src="(.*?)".*?alt="(.*?)"', re.DOTALL)

def recupAnimes():
        global p_html_anime
        
        try:
##                t0 = datetime.datetime.now()
                url = 'www.otaku-attitude.net'
                filename = 'animes.xml'
                html = ''
                fiches = []   
                monXML = []
                newEntry = False
                erreur = 0
                ajout = 0
                idAnim = 0
                alphabet = list(map(chr,range(65,91)))
                alphabet.insert(0,'.')
                print(alphabet)
                #print('\n[*] Starting recupAnimes() -> '+filename)
                monXML.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
                monXML.append('<?xml-stylesheet type="text/xsl" href="animes.xsl"?>\n')
                monXML.append("<animes>\n")
                conn = http.client.HTTPConnection(url)
                for letter in alphabet:
                        try:
                                
                                conn.request("GET", ''.join(["/liste-dl-animes.php?lettre=",letter]))
                                rep = conn.getresponse()
                                html = rep.read().decode('utf8')
                                results = re.findall(p_html_anime,html)

                                for result in results:
                                        nom = ''.join(['<![CDATA[',utils.full_clean(result[2]),']]>'])
                                        image = result[1]
                                        fiche = result[0]
                                        #info = ''.join(["\t<anime>\n\t\t<id>",str(idAnim),"</id>\n\t\t<nom>",nom,"</nom>\n\t\t<image>",image,"</image>\n\t\t<fiche>",fiche,"</fiche>\n\t</anime>\n"])
                                        monXML.append(''.join(["\t<anime>\n\t\t<id>",str(idAnim),"</id>\n\t\t<nom>",nom,"</nom>\n\t\t<image>",image,"</image>\n\t\t<fiche>",fiche,"</fiche>\n\t</anime>\n"]))
                                        ajout += 1
                                        idAnim += 1
                                
                        except:
##                              print("[-] Error : "+ str(sys.exc_info()[0]))
                              return False
                monXML.append('</animes>\n')
                conn.close()

                with open(filename, "w", encoding='utf-8') as fichier:
                        fichier.writelines(monXML)
                        fichier.close()

##                print("Total > "+ str(datetime.datetime.now() -t0))
                
        except KeyboardInterrupt:
##                print("Total > "+str(datetime.datetime.now() - t0))
                conn.close()
                exit(1)

#recupAnimes()
#recupAnimes()
#recupAnimes()
#recupAnimes()
#recupAnimes()
